package com.afteryuan.util.code;

import com.afteryuan.util.FileUtil;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
 * User: work25
 * Date: 2008-5-5
 * Time: 11:44:07
 */
public class GenerateServiceImplCode {

    /**
     * 生成Impl文件
     *
     * @param targetRoot      要创建的文件所在文件夹路径
     * @param modelLocation   模型文件位置，用来import的
     * @param serviceLocation service接口位置，用来import的
     * @param fileName        源文件名,带后缀不带路径
     */
    private void makeServiceImplFile(String targetRoot, String modelLocation, String serviceLocation, String fileName) {
        if (fileName.split("[.]").length > 1 && fileName.split("[.]")[1].equals("java")) {
            String suffix = fileName.split("[.]")[1];
            String modelName = fileName.split("[.]")[0];
            if (modelName.equals("BaseObject")) return;
            String serviceName = modelName + "Service";
            String serviceImplName = modelName + "ServiceImpl";
            File serviceImplFile = new File(targetRoot + "/" + serviceImplName + "." + suffix);
            if (!serviceImplFile.exists()) {
                try {
                    FileWriter fw = new FileWriter(serviceImplFile);
                    String packageName = targetRoot.replaceAll("/", ".");
                    packageName = packageName.substring(packageName.indexOf("com.kmpro.service.cicada.impl"));
                    fw.write("/**\n" +
                            " * Package:" + packageName + ";\n" +
                            " * $Id$\n" +
                            " * Copyright(c) 2001-2005 www.afteryuan.com\n" +
                            " */\n");

                    fw.write("package " + packageName + ";\n\n");
                    fw.write("import " + modelLocation + "." + modelName + ";\n");
                    fw.write("import " + serviceLocation + "." + serviceName + ";\n");
                    fw.write("import com.afteryuan.dao.HibernateEntityDao;\n");
                    fw.write("import org.apache.commons.logging.Log;\n");
                    fw.write("import org.apache.commons.logging.LogFactory;\n\n");
                    fw.write("/**\n" +
                            " * " + serviceImplName + "\n" +
                            " * <p/>\n" +
                            " * <p><a href=\"" + serviceImplName + ".java.html\"><i>View Source</i></a></p>\n" +
                            " *\n" +
                            " * @author <a href=\"mailto:afteryuan@gmail.com\">$" + "Author" + "$</a>\n" +
                            " * @version $" + "Revision" + "$\n" +
                            " */\n");
                    fw.write("public class " + serviceImplName + " extends HibernateEntityDao<" + modelName + "> implements " + serviceName + " {\n" +
                            "\n" +
                            "\tprivate static Log log = LogFactory.getLog(" + serviceImplName + ".class);\n" +
                            "}");
                    fw.close();
                    System.out.println("file " + serviceImplFile.getName() + " is built to " + serviceImplFile.getPath());
                } catch (IOException e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }
            }
        }
    }

    /**
     * 递归创建文件和文件夹等
     *
     * @param root       model所在文件夹路径
     * @param targetRoot 要创建的文件所在文件夹路径
     */
    public void buildFile(String root, String targetRoot) {
        File file = new File(root);
        String[] fileNames = FileUtil.getFileList(file);
        if (fileNames == null) return;
        String modelLocation = root.replaceAll("/", ".");
        modelLocation = modelLocation.substring(modelLocation.indexOf("com.kmpro.model.cicada"));
        String serviceLocation = targetRoot.replaceAll("/", ".");
        serviceLocation = serviceLocation.replaceAll(".impl", "");
        serviceLocation = serviceLocation.substring(serviceLocation.indexOf("com.kmpro.service.cicada"));
        for (String fileName : fileNames) {
            String newRoot = root + "/" + fileName;
            String newTargetRoot = targetRoot + "/" + fileName;
            File subFile = new File(newRoot);
            if (subFile.isDirectory()) {
                File targetDir = new File(newTargetRoot);
                if (!targetDir.exists() && !(newTargetRoot.indexOf("svn") > 0)) {
                    targetDir.mkdirs();
                    buildFile(newRoot, newTargetRoot);
                }
            } else {
                makeServiceImplFile(targetRoot, modelLocation, serviceLocation, fileName);
            }
        }
    }
}
